##
rm(list=ls())
gc()


library(data.table)
library(ggplot2)
library(dplyr)
library(stringr)



##### get party switching rates


data = rbindlist(lapply(c('panel-analysis-2008-2012.csv.gz',
                          'panel-analysis-2012-2016.csv.gz',
                          'panel-analysis-2016-2020.csv.gz'), FUN = function(x){
                            d=fread(x, select = c('Party_year1','DemDiff','RepDiff'))[,Years:=str_replace(str_replace(x, '.csv.gz', ''),'panel-analysis-','')]
                            
                            
                            return(d)
                          }))
data[!Party_year1%in%c('Democrat','Republican'),Party_year1:='Non-Partisan']
data[DemDiff==1|(Party_year1=='Democrat' & DemDiff==0),Party_year2:='Democrat']
data[RepDiff==1|(Party_year1=='Republican' & RepDiff==0),Party_year2:='Republican']

data[!Party_year2%in%c('Democrat','Republican'),Party_year2:='Non-Partisan']
data[,DemDiff:=NULL]
data[,RepDiff:=NULL]

data[,Year1:=substr(Years,1,4)]
data[,Year2:=substr(Years,6,9)]

d = data[Years=='2016-2020']
p = d[,list(Total = .N),by=c('Party_year1','Party_year2')]
p[,N_1:=sum(Total),by = 'Party_year1']

p[,prob:= round(Total/N_1,3)]
library(dplyr)

p[,color := case_when(Party_year1 == 'Republican' & Party_year2 == 'Republican' ~ '1',
                      Party_year1 == 'Democrat' & Party_year2 == 'Democrat' ~ '2',
                      Party_year1 == 'Non-Partisan' & Party_year2 == 'Non-Partisan' ~ '3',
                      Party_year1 == 'Republican' & Party_year2 == 'Non-Partisan'|Party_year1=='Non-Partisan'&Party_year2=='Republican' ~ '4',
                      Party_year1 == 'Democrat' & Party_year2 == 'Non-Partisan'|Party_year1=='Non-Partisan'&Party_year2=='Democrat' ~ '5',
                      Party_year1 == 'Republican' & Party_year2 == 'Democrat'|Party_year1=='Democrat'&Party_year2=='Republican' ~ '6'
                      
)]  
p[Party_year1!=Party_year2 & Party_year1 %in% c('Democrat', 'Republican'), a:='c']
p[Party_year1!=Party_year2 & (Party_year2 =='Non-Partisan' | Party_year1 =='Non-Partisan'), a:='b']
p[Party_year1==Party_year2, a:='a']
p[,Years:='2016-2020']

p1620=p
## 2012-2016

d = data[Years=='2012-2016']
gc()


p = d[,list(Total = .N),by=c('Party_year1','Party_year2')]
p[,N_1:=sum(Total),by = 'Party_year1']

p[,prob:= round(Total/N_1,3)]

p[,color := ifelse(Party_year1 == 'Democrat', '1',ifelse(Party_year2=='Republican', '3', '2'))]

p[Party_year1!=Party_year2 & Party_year1 %in% c('Democrat', 'Republican'), a:='c']
p[Party_year1!=Party_year2 & (Party_year2 =='Non-Partisan' | Party_year1 =='Non-Partisan'), a:='b']
p[Party_year1==Party_year2, a:='a']

p[,color := case_when(Party_year1 == 'Republican' & Party_year2 == 'Republican' ~ '1',
                      Party_year1 == 'Democrat' & Party_year2 == 'Democrat' ~ '2',
                      Party_year1 == 'Non-Partisan' & Party_year2 == 'Non-Partisan' ~ '3',
                      Party_year1 == 'Republican' & Party_year2 == 'Non-Partisan'|Party_year1=='Non-Partisan'&Party_year2=='Republican' ~ '4',
                      Party_year1 == 'Democrat' & Party_year2 == 'Non-Partisan'|Party_year1=='Non-Partisan'&Party_year2=='Democrat' ~ '5',
                      Party_year1 == 'Republican' & Party_year2 == 'Democrat'|Party_year1=='Democrat'&Party_year2=='Republican' ~ '6'
                      
)]  
p[Party_year1!=Party_year2 & Party_year1 %in% c('Democrat', 'Republican'), a:='c']
p[Party_year1!=Party_year2 & (Party_year2 =='Non-Partisan' | Party_year1 =='Non-Partisan'), a:='b']
p[Party_year1==Party_year2, a:='a']

p[,Years:='2012-2016']

p1216=p
## 2008 - 2012 
d = data[Years=='2008-2012']

p = d[,list(Total = .N),by=c('Party_year1','Party_year2')]
p[,N_1:=sum(Total),by = 'Party_year1']

p[,prob:= round(Total/N_1,3)]

p[,color := ifelse(Party_year1 == 'Democrat', '1',ifelse(Party_year2=='Republican', '3', '2'))]

p[Party_year1!=Party_year2 & Party_year1 %in% c('Democrat', 'Republican'), a:='c']
p[Party_year1!=Party_year2 & (Party_year2 =='Non-Partisan' | Party_year1 =='Non-Partisan'), a:='b']
p[Party_year1==Party_year2, a:='a']

p[,color := case_when(Party_year1 == 'Republican' & Party_year2 == 'Republican' ~ '1',
                      Party_year1 == 'Democrat' & Party_year2 == 'Democrat' ~ '2',
                      Party_year1 == 'Non-Partisan' & Party_year2 == 'Non-Partisan' ~ '3',
                      Party_year1 == 'Republican' & Party_year2 == 'Non-Partisan'|Party_year1=='Non-Partisan'&Party_year2=='Republican' ~ '4',
                      Party_year1 == 'Democrat' & Party_year2 == 'Non-Partisan'|Party_year1=='Non-Partisan'&Party_year2=='Democrat' ~ '5',
                      Party_year1 == 'Republican' & Party_year2 == 'Democrat'|Party_year1=='Democrat'&Party_year2=='Republican' ~ '6'
                      
)]  
p[Party_year1!=Party_year2 & Party_year1 %in% c('Democrat', 'Republican'), a:='c']
p[Party_year1!=Party_year2 & (Party_year2 =='Non-Partisan' | Party_year1 =='Non-Partisan'), a:='b']
p[Party_year1==Party_year2, a:='a']

p[,Years:='2008-2012']


p0812=p


##### current results table

files = paste0('results/',c('current-results-2008-2012.Rdata',
                                        'current-results-2012-2016.Rdata',
                                        'current-results-2016-2020.Rdata'))



results = rbindlist(lapply(files, FUN=function(file){
  
  
  load(file)
  
  models = ls()[grepl('Model',ls())]
  
  l = rbindlist(lapply(models, FUN=function(x){
    # print(x)
    m = get(x)
    out = as.data.table(m$coefficient)
    out[,Covariate:=rownames(m$coefficient)]
    out[,Model:=x]
    
    
    
    return(out)
    
  }))
  
  l[,Years:=case_when(grepl('2008-2012',file)~ '2008-2012',
                      grepl('2012-2016',file)~ '2012-2016',
                      grepl('2016-2020',file)~ '2016-2020')]  
  l[,file:=file]
  
  
  return(l)
}))


results = results[Covariate %in% c('DemSpExpDiff_nohh','RepSpExpDiff_nohh')]


results[grepl('2008-2012',file),Years:='2008-2012']
results[grepl('2012-2016',file),Years:='2012-2016']
results[grepl('2016-2020',file),Years:='2016-2020']

results[grepl('ExpDems',Model),Subset:='Democrat']
results[grepl('ExpReps',Model),Subset:='Republican']
results[grepl('ExpOths',Model),Subset:='Non-Partisan']

results[grepl('DemSp',Model),Outcome:='Democrat']
results[grepl('RepSp',Model),Outcome:='Republican']


p=rbind(p0812,p1216,p1620,fill=T)
p[,Subset:=Party_year1]
p[,Outcome:=Party_year2]

t = merge(results[,c('Estimate', 'Years','Subset','Outcome')],
          p[,c('prob', 'Years','Subset','Outcome')])
t[Outcome=='Democrat',Exposure := 'Democratic']
t[Outcome=='Republican',Exposure:= 'Republican']
t[,`Starting\nparty`:=Subset]
t[,`Effect of\n10 p.p.\nincrease`:=Estimate/10]
t[,`Baseline\nprobability\nof outcome`:=prob]
t[,`Percentage\nincrease`:=paste0(round((`Effect of\n10 p.p.\nincrease`/`Baseline\nprobability\nof outcome`)*100,1),'%')]

t = t[,c('Outcome', 'Years', 'Exposure', 'Starting\nparty', 
         'Effect of\n10 p.p.\nincrease', 'Baseline\nprobability\nof outcome',
         'Percentage\nincrease')]
library(xtable)
t = t[order(`Years`, Exposure, `Starting\nparty`)]

print(xtable(t, digits = 4), file = 'tables/TabS9.tex')


rm(t,results)
# downstream effect


files = paste0('results/',c('future-results-2008-2012.Rdata',
                                        'future-results-2012-2016.Rdata'))



results = rbindlist(lapply(files, FUN=function(file){
  
  
  load(file)
  
  models = ls()[grepl('Model',ls())]
  
  l = rbindlist(lapply(models, FUN=function(x){
    # print(x)
    m = get(x)
    out = as.data.table(m$coefficient)
    out[,Covariate:=rownames(m$coefficient)]
    out[,Model:=x]
    
    
    
    return(out)
    
  }))
  
  l[,Years:=case_when(grepl('2008-2012',file)~ '2008-2012',
                      grepl('2012-2016',file)~ '2012-2016',
                      grepl('2016-2020',file)~ '2016-2020')]  
  l[,file:=file]
  
  
  return(l)
}))


results = results[Covariate %in% c('DemSpExpDiff_nohh','RepSpExpDiff_nohh')]


results[grepl('2008-2012',file),`Treatment Years`:='2008-2012']
results[grepl('2012-2016',file),`Treatment Years`:='2012-2016']
results[grepl('2008-2012',file),`Years`:='2012-2016']
results[grepl('2012-2016',file),`Years`:='2016-2020']


results[grepl('ExpDems',Model),Subset:='Democrat']
results[grepl('ExpReps',Model),Subset:='Republican']
results[grepl('ExpOths',Model),Subset:='Non-Partisan']

results[grepl('DemSp',Model),Outcome:='Democrat']
results[grepl('RepSp',Model),Outcome:='Republican']


t = merge(results[,c('Estimate','Treatment Years', 'Years','Subset','Outcome')],
          p[,c('prob', 'Years','Subset','Outcome')])
t[Outcome=='Democrat',Exposure := 'Democratic']
t[Outcome=='Republican',Exposure:= 'Republican']
t[,`Outcome Years`:=Years]
t[,`Starting\nparty`:=Subset]
t[,`Effect of\n10 p.p.\nincrease`:=Estimate/10]
t[,`Baseline\nprobability\nof outcome`:=prob]
t[,`Percentage\nincrease`:=paste0(round((`Effect of\n10 p.p.\nincrease`/`Baseline\nprobability\nof outcome`)*100,1),'%')]

t = t[,c('Outcome', 'Treatment Years', 'Outcome Years','Exposure', 'Starting\nparty', 
         'Effect of\n10 p.p.\nincrease', 'Baseline\nprobability\nof outcome',
         'Percentage\nincrease')]
library(xtable)
t = t[order(`Treatment Years`, Exposure, `Starting\nparty`)]
print(xtable(t, digits = 4), file = 'tables/TabS11.tex')


